<template>
<div>
  <div class="title2">分类管理</div>
<div class="custom-tree-container" style="margin-top:100px;">
<div class="block">
    <el-tree
    style="background-color: #EFF6FC;margin-left:100px;line-align:10px"
      :data="data"
      show-checkbox
      node-key="id"
      default-expand-all
      :expand-on-click-node="false">
      <span  class="tree" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
          style="margin-left:100px;"
            type="text"
            size="mini"
            @click="() => append(data)">
            新增
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="() => remove(node, data)">
            删除
          </el-button>
        </span>
      </span>
    </el-tree>
  </div>
</div>
</div>
</template>
<script>
  let id = 1000;

  export default {
    data() {
      const data = [{
        id: 1,
        label: '新品',
        children: [{
          id: 4,
          label: 'CGLDG系列',
          },
          {
          id: 5,
          label:'BZDZLLY系列',
        }]
      }, {
        id: 2,
        label: '高端定制',
        children: [{
          id: 6,
          label: '意式极简'
        }, {
          id: 7,
          label: '现代极简'
        }]
      }, {
        id: 3,
        label: '展厅现货',
        children: [{
          id: 8,
          label: '轻奢家居',
        }]
      }];
      return {
        data: JSON.parse(JSON.stringify(data))
  
      }
    },

    methods: {
      append(data) {
        const newChild = { id: id++, label: 'testtest', children: [] };
        if (!data.children) {
          this.$set(data, 'children', []);
        }
        data.children.push(newChild);
      },

      remove(node, data) {
        const parent = node.parent;
        const children = parent.data.children || parent.data;
        const index = children.findIndex(d => d.id === data.id);
        children.splice(index, 1);
      },

    }
  };
</script>

<style>
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
</style>